Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@janiscommerce/model

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janiscommerce/model

[![Build Status](https://travis-ci.org/janis-commerce/model.svg?branch=master)](https://travis-ci.org/janis-commerce/model) [![Coverage Status](https://coveralls.io/repos/github/janis-commerce/model/badge.svg?branch=master)](https://coveralls.io/github/ja

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

model

Build Status Coverage Status

Installation

npm install @janiscommerce/model

Client injection

The client injection is useful when you have a dedicated database per client. Using the public setter client, the client will be stored in the controller instance. All the controllers and models getted using that controller will have the client injected.

Database Dispatcher

The Model uses Database Dispatcher for getting the correct DBDriver for a Model. The DBDriver will perform all the queries to the database.

Configure Database connection with databaseKey

If you have the connection settings you should add a databaseKey getter in you Model.

class MyModel extends Model {

	get databaseKey() {
		return 'core';
	}
}

Database Dispatcher will try to use one of the following settings

  1. Using Settings, with settings in file /path/to/root/.janiscommercerc.json:
{
	"database": {
		"core": {
			"host": "http://my-host-name.org",
			"type": "mysql",
			// ...
		}
	}
}
  1. Using ENV variables:
DB_CORE_HOST = "http://my-host-name.org";
DB_CORE_DATABASE = "db-name";
DB_CORE_USER = "user";
DB_CORE_PASSWORD = "foo123456";

Database connection configurated with client injected

When your Model is a Client Model, and the database connection settings are in the client injected, you don't need to configurate the databaseKey. You can add settings for the fields in the connection, the fields are the following.

For settings the package use Settings.

FieldDefault valueDescription
clients.fields.read.typedbReadTypeThe type for DB Read (mylsq, mongodb)
clients.fields.read.hostdbReadHostThe host for DB Read
clients.fields.read.protocoldbReadProtocolThe database protocol for DB Read
clients.fields.read.databasedbReadDatabaseThe database name for DB Read
clients.fields.read.userdbReadUserThe database username for DB Read
clients.fields.read.passworddbReadPasswordThe database password for DB Read
clients.fields.read.portdbReadPortThe database port for DB Read
clients.fields.write.typedbWriteTypeThe type for DB Write (mylsq, mongodb)
clients.fields.write.hostdbWriteHostThe host for DB Write
clients.fields.write.protocoldbWriteProtocolThe database protocol for DB Write
clients.fields.write.databasedbWriteDatabaseThe database name for DB Write
clients.fields.write.userdbWriteUserThe database username for DB Write
clients.fields.write.passworddbWritePasswordThe database password for DB Write
clients.fields.write.portdbWritePortThe database port for DB Write

Example of settings:

// .janiscommercerc.json
{
	"clients": {
		"fields": {
			"read": {
				"type": "dbReadType",
				"host": "dbReadHost",
				"protocol": "dbReadProtocol",
				"database": "dbReadDatabase",
				"user": "dbReadUser",
				"password": "dbReadPassword",
				"port": "dbReadPort"
			},
			"write": {
				"type": "dbWriteType",
				"host": "dbWriteHost",
				"protocol": "dbWriteProtocol",
				"database": "dbWriteDatabase",
				"user": "dbWriteUser",
				"password": "dbWritePassword",
				"port": "dbWritePort"
			}
		}
	}
}

API

const items = await myModel.get(params)

  • Returns items from database Params is an optional Object with filters, order, paginator.
const items = await myModel.get({ filters: { status: 'active' } });

myModel.getPaged(params, callback)

  • Returns items from database using pages, the default limit is 500 items per page.
await myModel.getPaged({ filters: { status: 'active' } }, (items, page, limit) => {
	// items is an array with the result from DB
});

myModel.getTotals()

  • After performing a get() sometimes you need data of totals. This method returns an object with that information. Result object structure: pages: The total pages for the filters applied page: The current page limit: The limit applied in get total: The total number of items in DB for the applied filters

await myModel.get({ filters: { status: 'active' } });
const totals = await myModel.getTotals();
/**
	totals content:
	{
		pages: 3,
		page: 1,
		limit: 500,
		total: 1450
	}
*/

myModel.insert(item)

  • Insert an item in DB. This method is only for insert, will not update perform an update.
await myModel.insert({ foo: 'bar' });

const items = await myModel.get({ filters: { foo: 'bar' }});

/**
	itemInserted content:
	[
		{
			foo: 'bar'
		}
		//...
	]
*/

myModel.save(item)

  • Insert/update an item in DB. This method will perfrom an upsert.
await myModel.save({ foo: 'bar' });

const items = await myModel.get({ filters: { foo: 'bar' }});

/**
	items content:
	[
		{
			foo: 'bar'
		}
		//...
	]
*/

myModel.update(values, filter)

  • Update items that match with the filter.
await myModel.update({ updated: 1 }, { status: 5 });
// will set updated = 1 for the items that has status = 5

myModel.remove(item)

  • Remove an item from DB.
await myModel.remove({ foo: 'bar' });

const items = await myModel.get({ filters: { foo: 'bar' }});

/**
	items content:
	[]
*/

myModel.multiInsert(items)

  • Perform a bulk insert of items in DB. This action will insert elements, and will not update elements.
await myModel.multiInsert([{ foo: 1 }, { foo: 2 }]);

const items = await myModel.get();

/**
	items content:
	[
		{ foo: 1 },
		{ foo: 2 }
	]
*/

myModel.multiSave(items)

  • Perform a bulk save of items in DB. This action will insert/update (upsert) elements.
await myModel.multiSave([{ foo: 1 }, { foo: 2 }]);

const items = await myModel.get();

/**
	items content:
	[
		{ foo: 1 },
		{ foo: 2 }
	]
*/

myModel.multiRemove(filter)

  • Perform a bulk remove of items in DB.
await myModel.multiRemove({ status: 2 });

const items = await myModel.get({ filters: { status: 2 }});

/**
	items content:
	[]
*/

FAQs

Package last updated on 31 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc